package cn.kooki.app.duobao.ui.Activity.Goods;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.data.Bean.User.record.RecordItem;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.ui.Activity.Comm.RootAcitivity;
import cn.kooki.app.duobao.ui.widget.ActionBarMenu;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.manager.AppManager;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import de.greenrobot.event.EventBus;

/**
 * Created by xiong on 2015/12/29.
 */
public class MyCodeAcitvity extends BaseNavActivity implements View.OnClickListener {
    @Bind(R.id.tv_mycode_goodsname)
    TextView tvMycodeGoodsname;
    @Bind(R.id.mycode_list_activity)
    TextView mycodeListActivity;
    @Bind(R.id.tv_mycode_goodsjoins)
    TextView tvMycodeGoodsjoins;
    @Bind(R.id.gd_mycodeList)
    GridView gdMycodeList;
    private ActionBarMenu barMenu;
    private RecordItem recordItem;
    private String code;
    private ArrayList<String> codes;


    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_mycode);
        ButterKnife.bind(this);
    }

    @Override
    protected void afterView() {
        setTitle("抢宝号码");
        int size = ShopCartManager.getInstance().getCartsSize();
        if (size > 0) {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow_withnew);
        } else {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow);
        }
        topRefresh.setVisibility(View.VISIBLE);
        topRefresh.setOnClickListener(this);
        recordItem = getIntent().getParcelableExtra("item");
        tvMycodeGoodsname.setText(recordItem.title);
        mycodeListActivity.setText("期号 : " + recordItem.qishu);
        SpannableString jonin = new SpannableString("参与了" + recordItem.q_num + "人次，下面是该期参与的全部夺宝号码");
        jonin.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.main_red)), 3, 3 + String.valueOf(recordItem.q_num).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvMycodeGoodsjoins.setText(jonin);
        code = getIntent().getStringExtra("code");
        codes = getIntent().getStringArrayListExtra("codes");
        String[] datas = null;
        if (TextUtils.isEmpty(code)){
            datas = codes.toArray(new String[]{});
        }else{
            datas = code.split(",");
        }
        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.layout_mycodelist_item, R.id.tv_mycodeItem, datas);
        gdMycodeList.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        if (v == topRefresh) {
            showMenu();
        }
    }

    private void showMenu() {
        if (barMenu == null) {
            barMenu = new ActionBarMenu(this);
            barMenu.setActionMenuListener(new ActionBarMenu.ActionBarMenuClickListener() {
                @Override
                public void onItemClick(int index) {
                    ChangePageEvent pageEvent = new ChangePageEvent();
                    if (index == 0) {
                        pageEvent.item = 3;
                    } else {
                        pageEvent.item = 0;
                    }
                    EventBus.getDefault().post(pageEvent);
                    AppManager.getAppManager().getTopWith(RootAcitivity.class);
                }
            });
        }
        barMenu.showAsDropDown(topRefresh, -ScreenUtils.dpToPxInt(this, 80), 0);
    }
}
